package com.foreknow.domo17;

import java.io.*;

/**
 * 对象输入流与对象输出流
 * ObjectInputStream
 * ObjectOutputStream
 */
public class ObjectOutputStreamDemo {
    public static void main(String[] args) {
        try {
            //            FileOutputStream fos = new FileOutputStream("data.ser"); //data.ser为二进制文件
            //            ObjectOutputStream oos = new ObjectOutputStream(fos);
            //            //将对象写入到data.ser二进制文件中
            //            Employee e1 = new Employee("张三",15,"市场部");
            //            Employee e2 = new Employee("李四",25,"开发部");
            //            oos.writeObject(e1);
            //            oos.writeObject(e2);
            //            oos.close();
            //读取文件中的对象信息
            FileInputStream fis = new FileInputStream("data.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Employee e1 = (Employee) ois.readObject();//读取文件中的对象
            Employee e2 = (Employee) ois.readObject();
            e1.showInfo();
            e2.showInfo();
            ois.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
